
//
//  YTHomeController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/9.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHomeController.h"
#import "YTHomeTitleView.h"
#import "YTHomeViewModel.h"
#import "YTHomeDetailController.h"
#import "YTNewUserGift.h"
#import "YTSearchController.h"
#import "YTGiftView.h"
#import "YTGuildeView.h"
#import "SGPagingView.h"
#import "YTHomeMainController.h"
#import "YTHomeFollowController.h"

@interface YTHomeController ()<UIScrollViewDelegate,
                                YTLoginViewControllerDelegate,
                                    SGPageTitleViewDelegate,
                                SGPageContentScrollViewDelegate>





@property(nonatomic ,assign)int currentPage; // 记录当前页

@property(nonatomic ,strong)YTHomeViewModel *viewModel;

@property(nonatomic ,strong)YTGiftView *giftView;

@property(nonatomic ,strong)SGPageTitleView *pageTitleView;

@property(nonatomic ,strong)SGPageContentScrollView *pageContentScrollView;

@end

@implementation YTHomeController


// lazy viewModel
- (YTHomeViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YTHomeViewModel alloc] init];
    }
    return _viewModel;
}
// lazy giftView
- (YTGiftView *)giftView
{
    if (!_giftView) {
        _giftView = [YTGiftView giftViewWithFrame:kScreenBounds];
        
        __weak typeof(self) WeakSelf = self;
        _giftView.openClickBlock = ^{
            YTLoginViewController *logVC = [[YTLoginViewController alloc] init];
            logVC.showTipDelegate = WeakSelf;
            [WeakSelf.navigationController pushViewController:logVC animated:true];
        };
        
        _giftView.closeBlock = ^{
            
            YTGuildeView *view = [[YTGuildeView alloc] init];
            view.frame = kScreenBounds;
            [UIApplication.sharedApplication.keyWindow addSubview:view];
        };
    }
    return _giftView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setupNav];
    

    
    [self loadCateList];
    
    // 注册通知
    // 注册进入后台通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationEnterBackground)
                                                 name: UIApplicationDidEnterBackgroundNotification
                                               object:nil];
    
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(showPushDetailVC:)
                                               name:kHomePushNotificationName
                                             object:nil];
    
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(showGiftView)
                                               name:@"kShowFirstDLNotification" object:nil];


}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.subviews[0].subviews[0].hidden = YES;
    [MobClick beginLogPageView:@"首页"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.subviews[0].subviews[0].hidden = false;
    [MobClick endLogPageView:@"首页"];

}

- (void)showGiftView
{
    [UIApplication.sharedApplication.keyWindow addSubview:self.giftView];
}


#pragma mark: - 推送文章 的通知
- (void)showPushDetailVC:(NSNotification *)n
{
    BOOL isArticlePush = [n.userInfo[@"push_isArticle_type"] boolValue];
    NSString *ID = n.userInfo[@"news_id"];
    UIViewController *detailVC = isArticlePush ? [[YTHomeDetailController alloc] init] : [YTDetailNewController controller];
    isArticlePush ? [(YTHomeDetailController *)detailVC setID:ID] : [(YTDetailNewController *)detailVC setID:ID];
    [self.navigationController pushViewController:detailVC animated:true];
}

// 程序进入后台通知 监听
- (void)applicationEnterBackground
{
    if ([UIViewController isCurrentViewControllerVisible:self]) {
        [[NSUserDefaults standardUserDefaults] setObject:kHomePushNotificationName forKey:YTPushNotificationName];
    }
}


- (void)dealloc
{
    
    [NSNotificationCenter.defaultCenter removeObserver:self
                                                  name:UIApplicationDidEnterBackgroundNotification
                                                object:nil];
    
    [NSNotificationCenter.defaultCenter removeObserver:self
                                                  name:@"kShowFirstDLNotification"
                                                object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self
                                                  name:kHomePushNotificationName
                                                object:nil];
}

//- (void)showArticleDetailNoti:(NSNotification *)n
//{
//    NSString *ID = n.userInfo[@"ID"];
//    YTHomeDetailController *dtVc = [[YTHomeDetailController alloc] init];
//    dtVc.ID = ID;
//    [self.navigationController pushViewController:dtVc animated:true];
//}


#pragma mark: - 加载分类
- (void)loadCateList
{
    
   // __weak typeof(self) WeakSelf = self;
    [self.viewModel loadCateListWithCompletion:^(BOOL isSucc, NSString *error) {
        if (error) {
            [self.view showTipWithTipStr:error];
            return ;
        }
        
        //[self.pageTitleView ]
        /// pageTitleViewConfigure
        SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
        configure.indicatorStyle = SGIndicatorStyleDynamic;
        configure.titleSelectedColor = kMainColor;
        configure.indicatorColor = kMainColor;
        configure.titleTextZoom = YES;
        configure.titleTextZoomRatio = 0.5f;
        configure.titleGradientEffect = YES;
        configure.titleAdditionalWidth = 35;
        configure.indicatorHeight = 3.0f;
        configure.indicatorCornerRadius = 2.0f;
        
        NSArray *arrayList = [@[@"关注"] arrayByAddingObjectsFromArray:[self.viewModel.cateList valueForKey:@"name"]];
        
        /// pageTitleView
        self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, KIsiPhoneX ? 88 : 64, kScreenWidth, 44)
                                                            delegate:self
                                                          titleNames:arrayList
                                                           configure:configure];
        [self.view addSubview:self.pageTitleView];
        self.pageTitleView.selectedIndex = 1;
        
        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:5];
        for (int i = 0; i < self.viewModel.cateList.count + 1; i++)
        {
            if (i != 0) {
                YTHomeMainController *vc = [[YTHomeMainController alloc] init];
                vc.isHot = (i == 1);
                vc.term_id = self.viewModel.cateList[i - 1].term_id;
                [arrayM addObject:vc];
            }else{
                // 关注
                YTHomeFollowController *vc = [[YTHomeFollowController alloc] init];
                [arrayM addObject:vc];
            }
  
        }
        
        /// pageContent
        CGFloat pageContentViewY = CGRectGetMaxY(self.pageTitleView.frame);
        self.pageContentScrollView = [[SGPageContentScrollView alloc] initWithFrame:CGRectMake(0, pageContentViewY, kScreenWidth, kScreenHeight - pageContentViewY - (KIsiPhoneX ? 83 : 49))
                                                                           parentVC:self
                                                                           childVCs:arrayM.copy];
        self.pageContentScrollView.delegatePageContentScrollView = self;
        [self.view addSubview:self.pageContentScrollView];
        
        
    }];
    
    
}

#pragma mark: - YTLoginViewControllerDelegate
- (void)loginVCShowTip:(YTLoginViewController *)loginVC tip:(NSString *)tip
{
    if ([tip isEqualToString:@"注册成功"]) {
        YTNewUserGift *gift = [YTNewUserGift userGiftViewWithFrame:kScreenBounds];
        [self.view addSubview:gift];
        return;
    }

    [self.view showTipWithTipStr:tip];
}

- (void)setupNav
{
    self.navigationItem.title = @"";
    
    // 设置左边logo
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_logo"]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imgView];
    
    [self.navigationItem.leftBarButtonItem setWidth:78.5];
    
    // 设置右边搜索按钮
    UIBarButtonItem *flexiButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage dh_setNavImageName:@"icon_home_search"] style:UIBarButtonItemStylePlain target:self action:@selector(searchButtonClick)];

    flexiButton.width = 15;

    self.navigationItem.rightBarButtonItems = @[flexiButton ,searchButton];

    
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:171.0f/255.0f green:126.0f/255 blue:128.0f/255.0f alpha:1]];
    
    [self.navigationController.navigationBar setBarTintColor:UIColor.whiteColor];
}



#pragma mark: - 搜索按钮监听
- (void)searchButtonClick
{
    
    PYSearchViewController *searchVC = [PYSearchViewController searchViewControllerWithHotSearches:[self.viewModel.hotSearchList valueForKey:@"keyword"] searchBarPlaceholder:@"请输入要搜索的文章关键字" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText)
    {
        YTSearchController *vc = [[YTSearchController alloc] init];
        vc.keyWord = searchText;
        [searchViewController.navigationController pushViewController:vc animated:true];
    }];
    
    searchVC.searchHistoryStyle = PYSearchHistoryStyleNormalTag;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchVC];
    [self presentViewController:nav animated:NO completion:nil];
}





- (void)pageTitleView:(SGPageTitleView *)pageTitleView
        selectedIndex:(NSInteger)selectedIndex
{
    [self.pageContentScrollView setPageContentScrollViewCurrentIndex:selectedIndex];
    
}
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView
                     progress:(CGFloat)progress
                originalIndex:(NSInteger)originalIndex
                  targetIndex:(NSInteger)targetIndex
{
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
    
}

- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView index:(NSInteger)index
{
    NSLog(@"%zd",index);
}


// 设置界面
- (void)setupUI
{

    
}


@end
